package com.LeeCode;

/**
 * 求根节点到叶节点数字之和
 */

public class Code129 {
    public static void main(String[] args) {
        Integer[] arr = {4};
        TreeNode root = Utils.buildTree(arr);
        System.out.println(new Code129().sumNumbers(root));
    }

    private int ans = 0;

    public int sumNumbers(TreeNode root) {
        dfs(root, 0);
        return ans;
    }

    private void dfs(TreeNode root, int num) {
        if (root == null) return;
        if (root.left == null && root.right == null) {
            ans += num + root.val;
        }
        dfs(root.left, 10 * (num + root.val));
        dfs(root.right, 10 * (num + root.val));
    }
}
